package zmee.pur.src.formplugin;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: xw
 * @Date: 2022/11/29/17:06
 * @Description: 提交校验项目成员业务角色
 */
public class ProjectBillPlugin extends AbstractBillPlugIn {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate formOperate = (FormOperate) args.getSource();
        String operateKey = formOperate.getOperateKey();
        if ("submit".equals(operateKey)) {
            DynamicObject sourceType = (DynamicObject) this.getModel().getValue("sourcetype");
            if (sourceType == null) {
                return;
            }
            String number = sourceType.getString("number");
            if ("C010405".equals(number) || "C010406".equals(number) || "C010415".equals(number) || "C010418".equals(number)) {
                Object id = this.getModel().getValue("id");
                QFilter filter = new QFilter("parentid", QCP.equals, id);
                DynamicObject projectMan = BusinessDataServiceHelper.loadSingle("src_project_man", "entryentity.bizrole", filter.toArray());
                if (projectMan != null) {
                    DynamicObjectCollection entryEntity = projectMan.getDynamicObjectCollection("entryentity");
                    Set<String> bizRole =
                            entryEntity.stream().map(e -> e.getDynamicObject("bizrole").getString("number")).collect(Collectors.toSet());
                    if (!bizRole.contains("SYS022") || !bizRole.contains("JS0001")) {
                        this.getView().showMessage("项目成员需包含【开标监管人】、【开标主持人】，请检查项目成员信息！");
                        args.setCancel(true);
                    }
                }
            }
        }
    }
}